<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/question.php';

class QuestionDao {
	private $_db;

	public function __construct() {
		$this->_db = new DbConnection();
	}

	/*
	 * sprintf("CALL sp_AddItem('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%u', '%u', '%s', '%s', '%s', @ItemId);",
					$item->getItem(),
					$item->getUserId(
	 */
	public function getQuestionProviderList($cat){		
		$query = sprintf("SELECT * FROM questions_provider WHERE CategoryId = '%s'", $cat);		
		$result = $this->_db->executeQuery($query);
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}		
		
		$questionList = array();
		
		while ($row = mysql_fetch_assoc($result)) {
						
			$question = new QuestionModel();
			
			$question->setQuestionID($row['QuestionId']);
			$question->setQuestion($row['Question']);
			$question->setCategoryID($row['CategoryId']);
			$question->setAnswers($row['Answers']);
			
			$questionList[] = $question;
		}
		
		mysql_free_result($result);		

		return $questionList;
	}

	public function getQuestionItemList($arr){		
		$query = 'SELECT * FROM questions_item';
		if (isset($arr) && count($arr) > 0) {
			$query .= ' WHERE CategoryId IN ( ';
			for ($i = 0; $i < count($arr) - 1; $i++) {
				$query .= $arr[$i] . ', ';
			}
			$query .= $arr[count($arr) - 1] . ' )';
		}
		
		$result = $this->_db->executeQuery($query);
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}		
		
		$questionList = array();
		
		while ($row = mysql_fetch_assoc($result)){
			$question = new QuestionModel();
			
			$question->setQuestionID($row['QuestionId']);
			$question->setQuestion($row['Question']);
			$question->setCategoryID($row['CategoryId']);
			$question->setAnswers($row['Answers']);
			
			$questionList[] = $question;
		}
		mysql_free_result($result);
		
		return $questionList;
	}
	
	public function getQuestionProvider($questionID) {
		$query = sprintf("SELECT * FROM questions_provider WHERE QuestionId = '%u'", $questionID);
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		if ($row = mysql_fetch_assoc($result)) {
			$question = new QuestionModel();
			
			$question->setQuestionID($questionID);
			$question->setQuestion($row['Question']);
			$question->setCategoryID($row['CategoryId']);
			$question->setAnswers($row['Answers']);
		}
		return $question;
	}
	
	public function getQuestionItem($questionID) {
		$query = sprintf("SELECT * FROM questions_item WHERE QuestionId = '%u'", $questionID);
	
		$result = $this->_db->executeQuery($query);
		
		if ($row = mysql_fetch_assoc($result)) {
			$question = new QuestionModel();
			
			$question->setQuestionID($questionID);
			$question->setQuestion($row['Question']);
			$question->setCategoryID($row['CategoryId']);
			$question->setAnswers($row['Answers']);			
		}		
			
		return $question;
	}
}
?>